package com.busines.alipay;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.common.util.SysParamUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.xiongyingqi.qrcode.QRCode;

public class AliPayApi {

	private static DefaultAlipayClient alipayClient;
	
	public static String format = "json";
	public static String charset = "UTF-8";
	public static String server_url = SysParamUtil.getParam("server_url");
	public static String app_id = SysParamUtil.getParam("app_id");
    public static String private_key = SysParamUtil.getParam("private_key");
	public static String public_key=SysParamUtil.getParam("public_key");
	public static String sign_type=SysParamUtil.getParam("sign_type");
	public static String NOTIFY_DOMAIN=SysParamUtil.getParam("notify_domain");
	static{
		alipayClient = new DefaultAlipayClient(server_url,app_id,private_key,format,charset,public_key,sign_type);
	}
	 /**
     * 扫码支付
     * https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.i0UVZn&treeId=193&articleId=105170&docType=1#s4
     * @param notifyUrl 
     * @return
     * @throws AlipayApiException 
     */
    public static String tradePrecreatePay(AlipayTradePrecreateModel model, String notifyUrl) throws AlipayApiException{
        AlipayTradePrecreateResponse response = tradePrecreatePayToResponse(model,notifyUrl);
        return response.getBody();
    }
    public static AlipayTradePrecreateResponse tradePrecreatePayToResponse(AlipayTradePrecreateModel model, String notifyUrl) throws AlipayApiException{
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setBizModel(model);
        request.setNotifyUrl(notifyUrl);
        return alipayClient.execute(request);
    }
    
    public static Map<String, String> toMap(HttpServletRequest request) {
        System.out.println(">>>>" + request.getQueryString());
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            // 乱码解决，这段代码在出现乱码时使用。
            // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        return params;
    }
    public static String tradeQuery(AlipayTradeQueryModel model) throws AlipayApiException{
    	AlipayTradeQueryResponse reponse = tradeQueryToResponse(model);
    	return reponse.getBody();
    }
    
    public static AlipayTradeQueryResponse tradeQueryToResponse(AlipayTradeQueryModel model) throws AlipayApiException{
    	AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
    	request.setBizModel(model);
    	return alipayClient.execute(request);
    }
    
    public static BufferedImage createImage(String url,int width,int height,String logofile) throws Exception{
    	QRCode qr = new QRCode();
    	//BitMatrix bitMatrix = new BitMatrix(width,height);
    	BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height);
    	return qr.toBufferedImageContents(bitMatrix);
    }
}
